/*
 *	This file is part of Lounge-Cast.
 *
 *  Author:  	Markus Moormann
 *	E-mail:  	mmoorman@uni-osnabrueck.de
 *	Copyright:	University of Osnabrueck (Zentrum Virtuos)
 * 	Website:	www.uni-osnabrueck.de
 *	Date:		16.02.2011
 *
 *	Lounge-Cast is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *	Lounge-Cast is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with Lounge-Cast.  If not, see <http://www.gnu.org/licenses/>.
 */
package backend.service;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.LinkedList;
import java.util.List;

import org.apache.log4j.Logger;

import backend.dbConnection.ConnectionHelper;
import backend.vo.CategoryVO;

public class CategoryService {
	
	/**
	 * a logger to log errors, debug information, etc.
	 */
	private static Logger LOG = Logger.getLogger(ContactService.class);
	/**
	 * the connection to database
	 */
	private Connection connection;
	/**
	 * the statement so send to database
	 */
	private Statement statement;
	
	public CategoryVO getParent(int category_id) {
		CategoryVO cat = null;
		try {
			connection = ConnectionHelper.getConnection();
			statement = connection.createStatement();
			
			String select = "SELECT " +
								"category_id," +
								"name," +
								"parent " +
							"FROM " +
								"categories " +
							"WHERE " +
								"category_id = " + category_id;
			ResultSet rs = statement.executeQuery(select);
			if(rs.next()) {
				cat = new CategoryVO(rs.getInt("category_id"),
									 rs.getString("name"),
									 rs.getInt("parent"));
			}
		} catch (Exception e) {
			LOG.error(e);
		} finally {
			ConnectionHelper.close(statement);
			ConnectionHelper.close(connection);
		}
		return cat;
	}
	
	public List<CategoryVO> getCategories() {
		List<CategoryVO> result = new LinkedList<CategoryVO>();
		String select = "SELECT " +
							"category_id," +
							"name," +
							"parent " +
						"FROM " +
							"categories ";
		try {
			connection = ConnectionHelper.getConnection();
			statement = connection.createStatement();
			
			ResultSet rs = statement.executeQuery(select);
			
			while(rs.next()) {
				CategoryVO cat = new CategoryVO(rs.getInt("category_id"),
									 rs.getString("name"),
									 rs.getInt("parent"));
				result.add(cat);
			}
			
		} catch (Exception e) {
			LOG.error(e);
		} finally {
			ConnectionHelper.close(statement);
			ConnectionHelper.close(connection);
		}
		
		
		return result;
	}

}
